package com.aurora.oa.work.view;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.aurora.oa.Constants.AuroraConstants;
import com.aurora.oa.R;
import com.aurora.oa.activity.BaseActivity;
import com.aurora.oa.adapter.CommonRecyclerViewAdapter;
import com.aurora.oa.adapter.CommonRecyclerViewHolder;
import com.aurora.oa.customview.ItemDivider;
import com.aurora.oa.message.bean.MyReceiveMessageBean;
import com.aurora.oa.message.presenter.MessageListActivityPresenter;
import com.aurora.oa.utils.DateUtil;
import com.aurora.oa.utils.ToastUtil;
import com.aurora.oa.work.bean.ExpenceBean;
import com.aurora.oa.work.presenter.WorkExpenceMainActivityPresenter;
import com.aurora.oa.work.presenter.WorkExpenceMainActivityPresenterImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/29 0029.
 */
public class WorkExpenceMainActivity extends BaseActivity implements WorkExpenceMainActivityView{
    private LinearLayoutManager linearLayoutManager;
    private Handler handler = new Handler();
    boolean isLoading;
    private int whitchPage =1;
    List<ExpenceBean> expenceDatas =new ArrayList<>();
    CommonRecyclerViewAdapter<ExpenceBean> commonRecyclerViewAdapter;
    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView rv_expence_list;
    private Button bt_new_expence;
    private WorkExpenceMainActivityPresenter presenter = new WorkExpenceMainActivityPresenterImpl(this);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setMyContentView(R.layout.activity_expence_main);
        initTitlebar(true,"报销",true);
        initUI();
        inttData();
    }

    private void inttData() {
        presenter.getExpencePageData(1);
        swipeRefreshLayout.setColorSchemeResources(R.color.blue,R.color.white);
        swipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(true);
            }
        });
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        whitchPage = 1;
                        expenceDatas.clear();
                        presenter.getExpencePageData(whitchPage);
                    }
                }, 0);
            }
        });
        commonRecyclerViewAdapter = new CommonRecyclerViewAdapter<ExpenceBean>(this,expenceDatas) {
            @Override
            public void convert(CommonRecyclerViewHolder h, ExpenceBean entity, int position) {
                h.setText(R.id.tv_expence_type,entity.expensetype);
               String data = DateUtil.getDateTime(entity.createtime);
                h.setText(R.id.tv_time,data);
                switch(entity.approvalstate){
                    case 1:
                        h.setText(R.id.tv_status,"未审核");
                        break;
                    case 2:
                        h.setText(R.id.tv_status,"审核通过");
                        break;
                    case 3:
                        h.setText(R.id.tv_status,"审核未通过");
                        break;
                }
            }

            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_expence_list;
            }
        };

        linearLayoutManager = new LinearLayoutManager(this);
        rv_expence_list.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                loadMoreData();
            }
        });
        rv_expence_list.setLayoutManager(linearLayoutManager);
        rv_expence_list.setAdapter(commonRecyclerViewAdapter);
        rv_expence_list.addItemDecoration(new ItemDivider().setDividerWith(15).setDividerColor(Color.TRANSPARENT));
        commonRecyclerViewAdapter.setOnRecyclerViewItemClickListener(new CommonRecyclerViewAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
             //   ToastUtil.show(WorkExpenceMainActivity.this,position+"");
               Intent intent =  new Intent(WorkExpenceMainActivity.this,ExpenceDetailActivity.class);
                intent.putExtra("id",expenceDatas.get(position).id);
                startActivity(intent);
            }
        });

        bt_new_expence.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(WorkExpenceMainActivity.this,AddExpenceActivity.class));
            }
        });
    }

    private void loadMoreData() {
        int lastVisibleItemPosition = linearLayoutManager.findLastVisibleItemPosition();
        if (lastVisibleItemPosition + 1 == commonRecyclerViewAdapter.getItemCount()) {
            boolean isRefreshing = swipeRefreshLayout.isRefreshing();
            if (isRefreshing) {
                commonRecyclerViewAdapter.notifyItemRemoved(commonRecyclerViewAdapter.getItemCount());
                return;
            }
            if (!isLoading) {
                isLoading = true;
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        whitchPage +=1;
                        commonRecyclerViewAdapter.removeFootView(0,false);
                        commonRecyclerViewAdapter.addFootView(View.inflate(WorkExpenceMainActivity.this,R.layout.item_foot,null));
                        commonRecyclerViewAdapter.notifyDataSetChanged();
                        presenter.getExpencePageData(whitchPage);
                    }
                }, 0);
            }
        }
    }

    private void initUI() {
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        rv_expence_list = (RecyclerView) findViewById(R.id.rv_expence_list);
        bt_new_expence = (Button) findViewById(R.id.bt_new_expence);
    }

    @Override
    public void onRequestDataFail(String s) {

    }

    @Override
    public void showPullDownRefresh(List<ExpenceBean> expenceDatas) {
        swipeRefreshLayout.setRefreshing(false);
        this.expenceDatas.addAll(expenceDatas);
        commonRecyclerViewAdapter.removeFootView(0,false);
        commonRecyclerViewAdapter.addFootView(View.inflate(this,R.layout.item_foot,null));
        commonRecyclerViewAdapter.notifyDataSetChanged();
        //防止页数太少不加载了，滑动的时候会自动判断，判断静止的时候就自己去加载了，0表示不滑动了
        loadMoreData();
    }

    @Override
    public void showPullUpLoading(List<ExpenceBean> expenceDatas) {
        swipeRefreshLayout.setRefreshing(false);
        isLoading = false;
        //没数据就把没数据foot显示出来
        if(expenceDatas==null||expenceDatas.size()==0){
            commonRecyclerViewAdapter.removeFootView(0,false);
            commonRecyclerViewAdapter.addFootView(View.inflate(this,R.layout.item_foot_null,null));
        }else{
            this.expenceDatas.addAll(expenceDatas);
        }
        commonRecyclerViewAdapter.notifyDataSetChanged();
    }
}
